#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Purpose: 
# Created: 04/02/2009
# $Id $
# Copyright (c) 2008,2009 by University College London
# Authors:
# Didrik Pinte --<dpinte@dipole-consulting.com>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with HydroPlatform for details.
'''
Exception related dialogs
'''
__version__ = "$Revision: 2700 $"

import sys
import wx

from Thuban import _

class ExceptionDialog(wx.Dialog):
    """
    Very slightly modified version of Thuban.UI.exceptiondialog module
    
    The exception dialog shows the exception message and then allows
    to either proceed with the application or to exit it. In the
    latter case, sys.exit(1) is applied immediately.
    """

    def __init__(self, parent, message, 
                 title = _('HydroPlatoform: Internal Error')):
        wx.Dialog.__init__(self, parent, -1, title,
                          wx.DefaultPosition,
                          style = wx.RESIZE_BORDER|wx.CAPTION|wx.DIALOG_MODAL)

        self.parent = parent
        self.dialog_layout(message)

    def dialog_layout(self, message):
        '''
        Layout of the exception dialog
        '''
        top_box = wx.BoxSizer(wx.VERTICAL)

        text_box = wx.TextCtrl(self, -1, message,
            style=wx.TE_READONLY|wx.TE_MULTILINE|wx.TE_LINEWRAP)
        wdw, wdh = (500, 300)
        text_box.SetSizeHints(wdw, wdh)
        text_box.SetSize((wdw, wdh))

        top_box.Add(text_box, 1, wx.EXPAND|wx.ALL, 10)

        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(wx.Button(self, wx.ID_OK, _("Proceed")), 0, wx.ALL, 4)
        box.Add(wx.Button(self, wx.ID_CANCEL, _("Exit HydroPlatform now")), 
                0, wx.ALL, 4)
        top_box.Add(box, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALL, 10)

        self.Bind(wx.EVT_BUTTON, self.on_ok, id=wx.ID_OK)
        self.Bind(wx.EVT_BUTTON, self.on_exit, id=wx.ID_CANCEL)

        self.SetAutoLayout(True)
        self.SetSizer(top_box)
        top_box.Fit(self)
        top_box.SetSizeHints(self)

    def on_ok(self, event):
        '''
        Close gracefully on ok
        '''
        self.EndModal(wx.ID_OK)

    def on_exit(self, event):
        '''
        End gracefully on exit
        '''
        sys.exit(1)

def run_exception_dialog(parent, message, 
                         title = _('HydroPlatform: Internal Error')):
    '''
    Method launching the exception dialog
    '''
    dialog = ExceptionDialog(parent, message, title)
    dialog.ShowModal()
    dialog.Destroy()
